home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-08-14 | 8.0 KB | 284 lines | [TEXT/MPS ] |
- // My3dSupport.c - QuickDraw 3d routines
- //
- // This file contains utility routines for QuickDraw 3d sample code.
- //
- // Created 27th Dec 1994, Nick Thompson, DEVSUPPORT
- //
- // Modification History:
- //
- // 12/27/94 nick initial version
- // 12/31/94 nick add support for error library routines
- // 10/01/95 nick adjustments for simple box program
- // 03/22/95 rdd added geometry sample code. Updated for B1C2e7
- // draw context and picking changes.
- // Press any digit 1 through 9 to select a trigrid
- // and hold down control to render the trigrid with
- // texture map.
- // 04/12/95 rdd deleted MyColorBoxFaces and other unused routines.
-
- #include <QuickDraw.h>
- #include <QDOffScreen.h>
-
-
- #include "TriGrid3DSupport.h"
- #include "GeometrySample.h"
-
-
- #include "QD3DDrawContext.h"
- #include "QD3DRenderer.h"
- #include "QD3DShader.h"
- #include "QD3DCamera.h"
- #include "QD3DLight.h"
- #include "QD3DGeometry.h"
- #include "QD3DGroup.h"
- #include "QD3DMath.h"
- #include "QD3DSet.h"
- #include "QD3DTransform.h"
-
-
-
- TQ3ViewObject MyNewView(WindowPtr theWindow)
- {
- TQ3Status myStatus;
- TQ3ViewObject myView;
- TQ3DrawContextObject myDrawContext;
- TQ3RendererObject myRenderer;
- TQ3CameraObject myCamera;
- TQ3GroupObject myLights;
-
- myView = Q3View_New();
-
- // Create and set draw context.
- if ((myDrawContext = MyNewDrawContext(theWindow)) == nil )
- goto bail;
-
- if ((myStatus = Q3View_SetDrawContext(myView, myDrawContext)) == kQ3Failure )
- goto bail;
-
- Q3Object_Dispose( myDrawContext ) ;
-
- // Create and set renderer.
-
- // this would use the interactive software renderer
- if ((myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeInteractive)) != nil ) {
- if ((myStatus = Q3View_SetRenderer(myView, myRenderer)) != kQ3Failure ) {
- Q3InteractiveRenderer_SetDoubleBufferBypass(myRenderer, kQ3True);
- }
- else {
- goto bail;
- }
- }
- else {
- // this would use the wireframe renderer
- if ((myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeWireFrame)) != nil ) {
- if ((myStatus = Q3View_SetRenderer(myView, myRenderer)) != kQ3Failure ) {
- }
- else {
- goto bail;
- }
- }
- }
-
- Q3Object_Dispose( myRenderer ) ;
-
- // Create and set camera.
- if ( (myCamera = MyNewCamera(theWindow)) == nil )
- goto bail;
-
- if ((myStatus = Q3View_SetCamera(myView, myCamera)) == kQ3Failure )
- goto bail;
-
- Q3Object_Dispose( myCamera ) ;
-
- // Create and set lights.
- if ((myLights = MyNewLights()) == nil )
- goto bail;
-
- if ((myStatus = Q3View_SetLightGroup(myView, myLights)) == kQ3Failure )
- goto bail;
-
- Q3Object_Dispose(myLights);
-
- // Done!!!
- return ( myView );
-
- bail:
- // If any of the above failed, then don't return a view.
- return ( nil );
- }
-
- //----------------------------------------------------------------------------------
-
- TQ3DrawContextObject MyNewDrawContext(WindowPtr theWindow)
- {
- TQ3DrawContextData myDrawContextData;
- TQ3MacDrawContextData myMacDrawContextData;
- TQ3ColorRGB clearColor;
- TQ3DrawContextObject myDrawContext ;
-
- // Set the background color.
- Q3ColorRGB_Set(&clearColor, 0.2, 0.0, 1.0);
-
- // Fill in draw context data.
- myDrawContextData.clearImageMethod = kQ3ClearMethodWithColor;
- myDrawContextData.clearImageColor.a = 1.0;
- myDrawContextData.clearImageColor.r = clearColor.r;
- myDrawContextData.clearImageColor.g = clearColor.g;
- myDrawContextData.clearImageColor.b = clearColor.b;
- myDrawContextData.paneState = kQ3False;
- myDrawContextData.maskState = kQ3False;
- myDrawContextData.doubleBufferState = kQ3True;
-
- myMacDrawContextData.drawContextData = myDrawContextData;
-
- myMacDrawContextData.window = (CGrafPtr) theWindow; // this is the window associated with the view
- myMacDrawContextData.library = kQ3Mac2DLibraryNone;
- myMacDrawContextData.viewPort = nil;
- myMacDrawContextData.grafPort = nil;
-
- // Create draw context and return it, if it’s nil the caller must handle
- myDrawContext = Q3MacDrawContext_New(&myMacDrawContextData) ;
-
- return myDrawContext ;
- }
-
- //----------------------------------------------------------------------------------
-
- TQ3CameraObject MyNewCamera(WindowPtr theWindow)
- {
- TQ3ViewAngleAspectCameraData perspectiveData;
- TQ3CameraObject camera;
-
- TQ3Point3D from = {-5.0, 5.0, 11.0 };
- TQ3Point3D to = { 0.0, 0.0, 0.0 };
- TQ3Vector3D up = { 0.0, 1.0, 0.0 };
-
- float fieldOfView = .52359333333;
- float hither = 0.001;
- float yon = 1000;
-
- TQ3Status returnVal = kQ3Failure ;
-
-
- perspectiveData.cameraData.placement.cameraLocation = from;
- perspectiveData.cameraData.placement.pointOfInterest = to;
- perspectiveData.cameraData.placement.upVector = up;
-
- perspectiveData.cameraData.range.hither = hither;
- perspectiveData.cameraData.range.yon = yon;
-
- perspectiveData.cameraData.viewPort.origin.x = -1.0;
- perspectiveData.cameraData.viewPort.origin.y = 1.0;
- perspectiveData.cameraData.viewPort.width = 2.0;
- perspectiveData.cameraData.viewPort.height = 2.0;
-
- perspectiveData.fov = fieldOfView;
- perspectiveData.aspectRatioXToY =
- (float) (theWindow->portRect.right - theWindow->portRect.left) /
- (float) (theWindow->portRect.bottom - theWindow->portRect.top);
-
- camera = Q3ViewAngleAspectCamera_New(&perspectiveData);
-
- return camera ;
- }
-
-
- //----------------------------------------------------------------------------------
-
- TQ3GroupObject MyNewLights()
- {
- TQ3GroupPosition myGroupPosition;
- TQ3GroupObject myLightList;
- TQ3LightData myLightData;
- TQ3PointLightData myPointLightData;
- TQ3DirectionalLightData myDirectionalLightData;
- TQ3LightObject myAmbientLight, myPointLight, myFillLight;
- TQ3Point3D pointLocation = { -10.0, 0.0, 10.0 };
- TQ3Vector3D fillDirection = { 10.0, 0.0, 10.0 };
- TQ3ColorRGB WhiteLight = { 1.0, 1.0, 1.0 };
-
- // Set up light data for ambient light. This light data will be used for point and fill
- // light also.
-
- myLightData.isOn = kQ3True;
- myLightData.color = WhiteLight;
-
- // Create ambient light.
- myLightData.brightness = .2;
- myAmbientLight = Q3AmbientLight_New(&myLightData);
- if ( myAmbientLight == nil )
- goto bail;
-
- // Create point light.
- myLightData.brightness = 1.0;
- myPointLightData.lightData = myLightData;
- myPointLightData.castsShadows = kQ3False;
- myPointLightData.attenuation = kQ3AttenuationTypeNone;
- myPointLightData.location = pointLocation;
- myPointLight = Q3PointLight_New(&myPointLightData);
- if ( myPointLight == nil )
- goto bail;
-
- // Create fill light.
- myLightData.brightness = .2;
- myDirectionalLightData.lightData = myLightData;
- myDirectionalLightData.castsShadows = kQ3False;
- myDirectionalLightData.direction = fillDirection;
- myFillLight = Q3DirectionalLight_New(&myDirectionalLightData);
- if ( myFillLight == nil )
- goto bail;
-
- // Create light group and add each of the lights into the group.
- myLightList = Q3LightGroup_New();
- if ( myLightList == nil )
- goto bail;
- myGroupPosition = Q3Group_AddObject(myLightList, myAmbientLight);
- if ( myGroupPosition == 0 )
- goto bail;
- myGroupPosition = Q3Group_AddObject(myLightList, myPointLight);
- if ( myGroupPosition == 0 )
- goto bail;
- myGroupPosition = Q3Group_AddObject(myLightList, myFillLight);
- if ( myGroupPosition == 0 )
- goto bail;
-
- Q3Object_Dispose( myAmbientLight ) ;
- Q3Object_Dispose( myPointLight ) ;
- Q3Object_Dispose( myFillLight ) ;
-
- // Done!
- return ( myLightList );
-
- bail:
- // If any of the above failed, then return nothing!
- return ( nil );
- }
-
- //----------------------------------------------------------------------------------
- TQ3GroupObject MyNewModel()
- {
- TQ3GroupObject myGroup = NULL;
- TQ3ShaderObject myIlluminationShader;
-
- // Create a group for the complete model.
- // do not use Q3OrderedDisplayGroup_New since in this
- // type of group all of the translations are applied before
- // the objects in the group are drawn, in this instance we
- // dont want this.
- if ((myGroup = Q3DisplayGroup_New()) != NULL ) {
-
- // Define a shading type for the group
- // and add the shader to the group
- myIlluminationShader = Q3PhongIllumination_New();
- Q3Group_AddObject(myGroup, myIlluminationShader);
- }
-
- // dispose of the objects we created here
- if( myIlluminationShader )
- Q3Object_Dispose(myIlluminationShader);
-
- // Done!
- return ( myGroup );
- }
-
-